
const user = {
    // 获取客户的资料
    getUserInforText: function (id, callback) {
        this.public('/admin/customers/read', {
            id: id
        }, callback);
    },

    //修改客户的资料
    changeUserInfor: function (id,img,nickname,realname,source,email,phone,company,city,qq,labels,remark, callback) {
        this.public('/admin/customers/update', {
            id:id,
            img:img,
            nickname:nickname,
            realname:realname,
            source:source,
            email:email,
            phone:phone,
            company:company,
            city:city,
            qq:qq,
            labels:labels, 
            remark:remark
        }, callback);
    },

    //添加星标记
    getStar:function(id,marked,nickname,callback){
          this.public('/admin/customers/markedOrBlocked',{
            id:id,
            marked:marked,
            nickname:nickname
          },callback);  
    },

    //拉黑
    getblocked:function(id,blocked,nickname,callback){
          this.public('/admin/customers/markedOrBlocked',{
              id:id,
              blocked:blocked,
              nickname:nickname
          },callback);
    },


    //获取标签列表
    getListLabels:function(page,limit,title,type,callback){
            this.public('/admin/labels',{
                page:page ,
                limit:limit,
                title:title,
                type:type
            },callback);
    },

    //公共请求的function
    public: function (methods, data, success, error) {
        let xhr = new XMLHttpRequest();
        let formData = new FormData();
        for (let key in data) {
            if(data[key] instanceof Array) {
                for(let a in data[key]){
                    if (Object.prototype.hasOwnProperty.call(data[key], a)) {
                        formData.append(key+"[]",data[key][a]);
                    }
                }
            } else {
                formData.append(key, data[key]);
            }
        }
        formData.append("authkey", localStorage.getItem("authkey"));
        formData.append("sessionid", localStorage.getItem("sessionid"));

        xhr.open("post", process.env.API_URL + methods);
        xhr.send(formData);
        xhr.onreadystatechange = function () {
            if (xhr.status === 200) {
                if (xhr.readyState === 4) {
                    let responseData = JSON.parse(xhr.responseText);
                    if (success) success(responseData);
                }
            } else {
                if (errorHandler) {
                    let responseData = JSON.parse(xhr.responseText);
                    if (error) error(responseData);
                } else {
                    alert("请求失败！" + xhr.responseText);
                }
            }
        };
    }
};

export default user;





